package org.juiceshop.gutil;

import java.util.Iterator;

public class IterableDynamicObject<T,E> extends DynamicObject<T> implements Iterable<E> {

	private Class<E> _elementType;
	
	
	protected IterableDynamicObject (T delegate, Class<E> elementType ) {
		super(delegate,null);
		_elementType = elementType;
	}
	
	@Override
	public Iterator<E> iterator() {
		return iterator(_elementType);
	}
	
	public Iterator<DynamicObject<E>> dynamicIterator() {
		return new DynamicIterator<E>(iterator());
	}
	
	public String join (Object delimiter) {
		return call("join",delimiter.toString()).toString();
	}
	

	private class DynamicIterator<X> implements Iterator<DynamicObject<X>> {
		Iterator<X> _iterator;
		
		private DynamicIterator(Iterator<X> iterator) {
			_iterator = iterator;
		}

		@Override
		public boolean hasNext() {
			return _iterator.hasNext();
		}

		@Override
		public DynamicObject<X> next() {
			X obj = _iterator.next();
			return DynamicObject.create(obj);
		}

		@Override
		public void remove() {
			_iterator.remove();
		}
	}
	
}
